﻿#include <iostream>
#include <vector>
using namespace std;


class Solution
{
public:
    // 494. 目标和
    /*int findTargetSumWays(vector<int>& nums, int target)
    {
        int sum = 0;
        for (auto num : nums)
            sum += num;
        int aim = (sum + target) / 2;
        if ((target + sum) % 2 == 1 || aim < 0)
            return 0;

        int n = nums.size();
        vector<vector<int>> dp(n + 1, vector<int>(aim + 1));
        dp[0][0] = 1;

        for (int i = 1; i <= n; i++)
            for (int j = 0; j <= aim; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i - 1])
                    dp[i][j] += dp[i - 1][j - nums[i - 1]];
            }

        return dp[n][aim];
    }*/

    // 优化
    int findTargetSumWays(vector<int>& nums, int target)
    {
        int sum = 0;
        for (auto num : nums)
            sum += num;
        int aim = (sum + target) / 2;
        if ((target + sum) % 2 == 1 || aim < 0)
            return 0;

        int n = nums.size();
        vector<int> dp(aim + 1);
        dp[0] = 1;

        for (int i = 1; i <= n; i++)
            for (int j = aim; j >= 0; j--)
                if (j >= nums[i - 1])
                    dp[j] += dp[j - nums[i - 1]];

        return dp[aim];
    }
};

int main()
{

    return 0;
}